package com.zy.serivice.impl;

import com.github.pagehelper.PageInfo;
import com.zy.bean.TAdmin;
import com.zy.bean.TRole;
import com.zy.bean.TRoleExample;
import com.zy.mapper.TAdminRoleMapper;
import com.zy.mapper.TRoleMapper;
import com.zy.service.TRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;

import javax.management.relation.Role;
import java.util.List;
import java.util.Map;

/**
 * @Author 赵阳
 * @Date 2023/11/26 19:15
 * @Version 1.5
 * @name 方法名
 */
@Service
public class TRoleServiceImpl implements TRoleService {
    @Autowired
    TRoleMapper tRoleMapper;
    @Autowired
    TAdminRoleMapper tAdminRoleMapper;

    @Override
    public PageInfo<TRole> listRolePage(Map<String, Object> paramMap) {
        String condition = (String) paramMap.get("condition");

        TRoleExample example = new TRoleExample();
        if (!"".equals(condition)) {
            example.createCriteria().andNameLike("%" + condition + "%");
        }

        List tRoles = tRoleMapper.selectByExample(example);
        PageInfo<TRole> page = new PageInfo<>(tRoles, 5);
        return page;
    }
   // @PreAuthorize("hasRole('PM - 项目经理')")
    @Override
    public void instRole(TRole role) {
        String newName = role.getName().replaceAll(" ", "");
        if (!"".equals(newName)) {
            role.setName(newName);
            int i = tRoleMapper.insertSelective(role);
        }

    }

    @Override
    public TRole selectbyId(Integer id) {
        TRole tRole = tRoleMapper.selectByPrimaryKey(id);
        return tRole;
    }

    @Override
    public void updateByPrimaryKeySelective(TRole tRole) {
        String newName = tRole.getName().replaceAll(" ", "");//去掉所有的空格
        tRole.setName(newName);
        int i = tRoleMapper.updateByPrimaryKeySelective(tRole);

    }

    @Override
    public TRole getRoleById(Integer id) {
        TRole tRole = tRoleMapper.selectByPrimaryKey(id);
        return tRole;
    }

    @Override
    public void delRole(Integer id) {
        tRoleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void delselectRole(List<Integer> list) {
        tRoleMapper.delselectRole(list);
    }

    @Override
    public List<TRole> listRole() {
        return tRoleMapper.selectByExample(null);
    }

    @Override
    public List<Integer> listRoleIdByAdmin(String id) {
        return tAdminRoleMapper.listRoleIdByAdmin(id);
    }


}
